package com.wry.sharding.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wry.sharding.dao.Orders;
import com.wry.sharding.dao.OrderInfo;
import com.wry.sharding.dao.Shoping;
import com.wry.sharding.service.OrderInfoService;
import com.wry.sharding.service.OrdersService;
import com.wry.sharding.service.ShopingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <h1>  <h1>
 *
 * @author wry
 * @since 2025/1/6 10:25
 */
@RequestMapping("test")
@RestController
public class TestController {

    @Resource
    private ShopingService shopingService;

    @GetMapping("insert")
    public String test01(){
        for (int i = 40; i <= 50; i++) {
            Shoping shoping = new Shoping();
            shoping.setShopingName("shopName-"+i);
            shoping.setShopingPrice(9666666);
            shopingService.save(shoping);
        }
        return "ok";
    }

    @Resource
    private OrdersService orderService;
    @Resource
    private OrderInfoService orderInfoService;
    @GetMapping("insert-join")
    public String test02(){
        Orders order = new Orders();
        order.setUserId(44909L);
        order.setOrderPrice(1500);
        orderService.save(order);
        System.out.println(JSON.toJSONString(order));
        for (int i = 1; i <= 3; i++) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderId(order.getOrderId());
            orderInfo.setShopingName("胜多负少-"+i);
            orderInfo.setShopingPrice(500);
            orderInfoService.save(orderInfo);
        }

        return "ok";
    }

    @GetMapping("query")
    public JSONObject query(){
        final Shoping one = shopingService.lambdaQuery()
                .eq(Shoping::getShopingName, "shopName-25")
                .last("limit 1")
                .one();
        return JSON.parseObject(JSON.toJSONString(one));
    }
}
